#!/usr/bin/perl

use strict;
use warnings;

use Getopt::Long qw(:config no_auto_abbrev no_ignore_case);
use Net::FTP;
use Number::Format;
use Pod::Usage;
use Term::ProgressBar;

our $VERSION = "0.1";

#===============================================================================

# change this if you want to use you own email and password
our $opt_login  = 'desintegr@gmail.com';    # must be a valid email
our $opt_passwd = "12345";                  # less than 20 charaters

#===============================================================================

our $host        = "dl.free.fr";
our $file        = $ARGV[0];
our $opt_help    = 0;
our $opt_verbose = 0;
our $opt_version = 0;

#===============================================================================

sub upload {

    # connect
    my $ftp;
    unless ( $ftp = Net::FTP->new($host) ) {
        warn "Error: could not connect to '$host'.\n";
        warn "Message: $@\n" if $opt_verbose;
        exit 2;
    }

    # login
    unless ( $ftp->login( $opt_login, $opt_passwd ) ) {
        warn "Error: could not login.\n";
        warn "Message: " . $ftp->message if $opt_verbose;
        exit 2;
    }

    # set binary mode
    $ftp->binary;

    print "Connected to '$host'.\n" if $opt_verbose;

    # get file size in bytes
    my $size = ( stat($file) )[7];

    # print filename and file size
    my $nf = Number::Format->new();
    print "Uploading '$file', size " . $nf->format_bytes( $size, 2 ) . "B\n\n";

    # prepare upload
    my $dataconn;
    unless ( $dataconn = $ftp->stor($file) ) {
        warn "Error: could not upload file.\n";
        warn "Message: " . $ftp->message if $opt_verbose;
        exit 2;
    }

    # upload data
    my $progress = Term::ProgressBar->new( { count => $size, remove => 1 } );

    open( my $input, "<", $file );
    my $total_bytes_read = 0;

    while ( my $bytes_read = read( $input, my $buffer, 1024 ) ) {
        $dataconn->write( $buffer, $bytes_read );
        $progress->update( $total_bytes_read += $bytes_read );
    }

    # close upload and file
    $dataconn->close;
    close($input);

    # print download link
    print(  "Download link: "
          . ( $ftp->message =~ /(http:\/\/dl.free.fr.*)/ )[0]
          . "\n" );

    # close connection
    $ftp->quit;
}

sub print_version {
    print "freeup $VERSION\n";
    print "A simple dl.free.fr upload script\n";
    print "\n";
    print "Written by Damien Gombault <desintegr\@gmail.com>\n";
    exit 1;
}

sub try_help_message {
    return "Try '$0 --help' for more information.\n";
}

sub freeup {
    GetOptions( "login|l=s", "passwd|p=s", "help|h", "verbose|v", "version" )
      or die try_help_message;

    pod2usage(1)  if $opt_help;
    print_version if $opt_version;

    die "Missing filename.\n" . try_help_message if @ARGV == 0;
    die "Too many filenames given.\n" . try_help_message if @ARGV > 1;

    upload;
}

#===============================================================================

freeup();

#===============================================================================

=head1 NAME

freeup - A simple dl.free.fr upload script

=head1 SYNOPSIS

freeup [OPTIONS]... [FILE]

 Options:
    -l, --login=LOGIN           login used for authentification
    -p, --passwd=PASSWORD       password used for autentification
    -v, --verbose               print more details

    -h, --help                  print this help and exit
        --version               print version and exit
    

=head1 DESCRIPTION

B<This program> will upload the given file on the free dl.free.fr file hosting
service.

=head1 AUTHOR

Damien Gombault <desintegr@gmail.com>

=cut

